/**
 * 显示模态对话框
 */
const showModal = (title, content, showCancel, cancelText, cancelColor, confirmText, confirmColor) => {
    return new Promise((resolve, reject) => {
        uni.showModal({
            title: title,
            content: content,
            showCancel: showCancel && true,
            cancelText: cancelText || '取消',
            cancelColor: cancelColor || '#000000',
            confirmText: confirmText || '确定',
            confirmColor: confirmColor || '#576B95',
            success(res) {
                if (res.confirm) {
                    resolve(true);
                } else if (res.cancel) {
                    // Android 6.7.2 及以上版本 和 iOS 点击蒙层不会关闭模态弹窗，所以尽量避免使用「取消」分支中实现业务逻辑
                    resolve(false);
                } else {
                    resolve(false);
                }
            },
            fail: (res) => {
                // Android 6.7.2 以下版本，点击取消或蒙层时，回调 fail, errMsg 为 "fail cancel"；
                reject(false);
            }
        });
    });
};




/**
 * showLoading
 * 显示 loading 提示框。需主动调用hideLoading 才能关闭提示框。
 */
const showLoading = async (title, mask = false) => {
    return new Promise((resolve, reject) => {
        uni.showLoading({
            title: title || '加载中',
            mask: mask,
            success(res) {
                resolve(true);
            },
            fail(res) {
                resolve(false);
            }
        })
    });
};

/**
 * hideLoading
 * 隐藏 loading 提示框。
 */
const hideLoading = async () => {
    return new Promise((resolve, reject) => {
        uni.hideLoading({
            success(res) {
                resolve(true);
            },
            fail(res) {
                resolve(false);
            }
        })
    });
};

/**
 * showLoading
 * 显示 loading 提示框。需主动调用hideLoading 才能关闭提示框。
 */
const showToast = async (title, icon = 'none', duration = 1500, mask = true, image) => {
    return new Promise((resolve, reject) => {
        uni.showToast({
            title: title,
            icon: icon,
            duration: duration,
            mask: mask,
            image: image || null,
            success(res) {
                resolve(true);
            },
            fail(res) {
                resolve(false);
            }
        })
    });
};

/**
 * hideToast
 * 隐藏消息提示框。
 */
const hideToast = async () => {
    return new Promise((resolve, reject) => {
        uni.hideToast({
            success(res) {
                resolve(true);
            },
            fail(res) {
                resolve(false);
            }
        })
    });
};

/**
 * alert
 * alert信息
 */
const alert = (showCancel, title, content, confirmColor) => {
    showCancel = !!showCancel;
    title = title || '';
    content = content || '';
    confirmColor = confirmColor || '#3CC51F';

    return new Promise((resolve, reject) => {
        uni.showModal({
            showCancel: showCancel,
            title: title,
            content: content,
            confirmColor: confirmColor,
            success: function (res) {
                if (res.confirm) {
                    resolve(true);
                } else if (res.cancel) {
                    resolve(false);
                }
            }
        });
    });
};
/**
	 * opt  object | string
	 * to_url object | string
	 * 例:
	 * this.Tips('/pages/test/test'); 跳转不提示
	 * this.Tips({title:'提示'},'/pages/test/test'); 提示并跳转
	 * this.Tips({title:'提示'},{tab:1,url:'/pages/index/index'}); 提示并跳转值table上
	 * tab=1 一定时间后跳转至 table上
	 * tab=2 一定时间后跳转至非 table上
	 * tab=3 一定时间后返回上页面
	 * tab=4 关闭所有页面，打开到应用内的某个页面
	 * tab=5 关闭当前页面，跳转到应用内的某个页面
	 */
const Tips = (opt, to_url) =>{
	if (typeof opt == 'string') {
				to_url = opt;
				opt = {};
			}
			let title = opt.title || '',
				icon = opt.icon || 'none',
				endtime = opt.endtime || 2000,
				success = opt.success;
			if (title) uni.showToast({
				title: title,
				icon: icon,
				duration: endtime,
				success
			})
			if (to_url != undefined) {
				if (typeof to_url == 'object') {
					let tab = to_url.tab || 1,
						url = to_url.url || '';
					switch (tab) {
						case 1:
							//一定时间后跳转至 table
							setTimeout(function() {
								uni.switchTab({
									url: url
								})
							}, endtime);
							break;
						case 2:
							//跳转至非table页面
							setTimeout(function() {
								uni.navigateTo({
									url: url,
								})
							}, endtime);
							break;
						case 3:
							//返回上页面
							setTimeout(function() {
								// #ifndef H5
								uni.navigateBack({
									delta: parseInt(url),
								})
								// #endif
								// #ifdef H5
								history.back();
								// #endif
							}, endtime);
							break;
						case 4:
							//关闭所有页面，打开到应用内的某个页面
							setTimeout(function() {
								uni.reLaunch({
									url: url,
								})
							}, endtime);
							break;
						case 5:
							//关闭当前页面，跳转到应用内的某个页面
							setTimeout(function() {
								uni.redirectTo({
									url: url,
								})
							}, endtime);
							break;
					}

				} else if (typeof to_url == 'function') {
					setTimeout(function() {
						to_url && to_url();
					}, endtime);
				} else {
					//没有提示时跳转不延迟
					setTimeout(function() {
						uni.navigateTo({
							url: to_url,
						})
					}, title ? endtime : 0);
				}
			}
};
/*
	 * 合并数组
	 */
const SplitArray = (list, sp) =>{
    if (typeof list != 'object') return [];
    if (sp === undefined) sp = [];
    for (var i = 0; i < list.length; i++) {
        sp.push(list[i]);
    }
    return sp;
};


export default {
    showModal,
    showLoading,
    hideLoading,
    showToast,
    hideToast,
    alert,
	Tips,
    SplitArray,
};
